using System;
using Gtk;
using Pango;

namespace gtkGui
{
	public delegate int GetSize();
    public class WatermarkTextView : TextView
	{
        private readonly string label;
        private readonly ScrolledWindow scrolled_window;
        
        public WatermarkTextView(string label, ScrolledWindow scrolled_window) : base()
        {
            this.label = label;
            this.scrolled_window = scrolled_window;
        }

        protected override bool OnScrollEvent(Gdk.EventScroll @event)
        {
            if(!IsRealized) {
                return false;
            }
            QueueDraw();
            return base.OnScrollEvent(@event);
        }
        
        protected override void OnSizeAllocated(Gdk.Rectangle rect)
        {
            if(!IsRealized) {
                return;
            }
            QueueDraw();
            base.OnSizeAllocated(rect);
        }
        
        private int GetHeight()
        {
            if(scrolled_window == null) {
                return Allocation.Height;
            }
            return scrolled_window.Child.Allocation.Height + (int)scrolled_window.Vadjustment.Value;
        }
        
        private int GetWidth()
        {
            if(scrolled_window == null) {
                return Allocation.Width;
            }
            return scrolled_window.Child.Allocation.Width + (int)scrolled_window.Hadjustment.Value;
        }

        protected override bool OnExposeEvent(Gdk.EventExpose @event)
        {
            if(!IsRealized) {
                return false;
            }

            Gdk.Window drawable = GetWindow(TextWindowType.Text);

            Pango.Layout layout = new Pango.Layout(this.PangoContext);
            layout.SetText(label);
            Pango.FontDescription fd = this.PangoContext.FontDescription.Copy();
            fd.Weight = Pango.Weight.Ultrabold;
            fd.Size = 50240;
            layout.FontDescription = fd;

            int layout_width, layout_height;
            layout.GetPixelSize(out layout_width, out layout_height);

            Gdk.GC mainGC = new Gdk.GC(drawable);
            byte c = 245;
            mainGC.RgbFgColor = new Gdk.Color(c, c, c);

            drawable.DrawLayout(mainGC,
                GetWidth() - layout_width,
                GetHeight() - layout_height + (int)(layout_height / 3),
                layout);

            return base.OnExposeEvent(@event);
        }
    }
}
